<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Finance  extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
	// 财务-充值
    public function recharge()
    {

    
     return $this->fetch();
    }


	// 财务-充值记录
    public function rechargeRecord()
    {
		if($this->request->isAjax()){
          $param = $this->request->param();
          $id  = !empty($param['id'])?intval($param['id']):''; //当前页

          $statusType =  isset($param['status'])?$param['status']:'all'; //请求状态
          $page  = !empty($param['page'])?intval($param['page']):''; //当前页
          $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
          $start = !empty($param['start'])?strtotime($param['start']):'';//开始
          $end   = !empty($param['end'])?strtotime($param['end']):''; //结束
          $page = ($page-1)*$limit; //起始位置

 //状态筛选
           switch($statusType)
           {
      
            case '0':
               //等待充值
              
               $where[] = ['status','=',0];
            break;
            case '1':
            // 充值成功
              
                $where[] = ['status','=',1];
            break;
            case '2':
            // 充值失败
             
                $where[] = ['status','=',2];
            break; 
         

           } 
         if(!empty($id)){
            $where[] = ['id','=',$id];
         }
        $where[] = ['uid','=',session('uid')]; 
        $order['create_time'] = 'desc';
    
        $count  =model('financeRecharge')->count($where);
        $list  = model('financeRecharge')->listData($where,"*", $order,"$page,$limit");

        foreach($list as $k=>$v){
          $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
          $list[$k]['pay_time']    = !empty($v['pay_time'])?date('Y-m-d H:i:s',$v['pay_time']):'--';
        }
 
        return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$list]);
     }else{
        return $this->fetch();
     }
    }

	// 财务-提现
    public function withdraw()
    {
      $param = $this->request->param();
      $step = !empty($param['step'])?intval($param['step']):1;
      if($step ==1){
        //第一步
          if(session('step') ==2){
             $this->redirect(url('finance/withdraw',['step'=>2]));
          }   
         if($this->request->isAjax()){
         $payword  = $param['payword'];  //安全密码
          $username = session('username'); //用户名
          if(empty($payword)){
             return error_json('请输入支付密码');
          }
          $paysalt = $this->user['paysalt']; //登陆用户的支付加密码
          if($this->user['payword']  != payword_key($username,$payword,$paysalt)){
             return error_json('支付密码错误');
          }
          session('step',2);
          return success_json('支付密码验证成功');

         }else{

               return $this->fetch();           
         }

 

      }elseif($step ==2){
        //第二步
          if(session('step') !=2){
             $this->redirect(url('finance/withdraw',['step'=>1]));
          }

          if($this->request->isAjax()){
            $param = $this->request->param();
              //验证
            $rule =[
              'bank_type'=>'require|integer',
              'amount'=>'require',
            ];
            $msg= [
             'bank_type.require'  =>'请选择提现方式', 
             'bank_type.integer'  =>'请选择正确的提现方式', 
             'amount.require' =>'请输入金额',

            ];
             $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
           if($error){
                  return error_json($error);
             }
            if(!is_numeric($param['amount'])){
             return error_json('请输入正确的金额');
            }
            
          // 检测提现金额是否满足条件
          if($param['amount'] < $this->settings['finance']['withdraw_min']  || $param['amount'] > $this->settings['finance']['withdraw_max']){
              return error_json('提现金额不满足单日提现额度');
          }
           if($this->user['money'] < $param['amount']){
             return error_json('余额不足，请充值'); 
           }
           $data['amount']     = $param['amount'];  //提现金额
           $data['bank_type'] = $param['bank_type']; //银行卡类型
           $data['uid']       = session('uid');
           $data['username']  = session('username');
           $data['create_time'] = time();
           $where['id'] = $data['bank_type'];
           $userBank = model('userBank')->infoData($where);

           $data['bankname']    = $userBank['bankname']; //银行卡名称
           $data['branch']      = $userBank['branch']; //支行
           $data['card_number'] = $userBank['card_number']; //卡号
           $data['realname']    = $userBank['realname']; //真实姓名
           $data['fee']         = $this->settings['finance']['withdraw_fee']?$this->settings['finance']['withdraw_fee']:0; //提现手续费
           $data['actual_account']    = $data['amount'] - $data['amount']*$data['fee']/100; //实际到账  = 实际金额- 实际金额 * 手续费             

           
           $data['ip']    = $this->request->ip(0,true); //真实IP

          if($param['__token__'] != session('__token__')){
            return error_json('请勿重复提交');
           }  
           model('financeWithdraw')->addData($data);
           $uid = session('uid');

         //  支出费用
          model('user')->payout($uid,$data['amount'],"提现","余额");
 
           return success_json('提现已申请，等待审核');

          }else{
              $where['uid'] = session('uid');
              $list = model('userBank')->listData($where);
              $this->assign('list',$list); 

             return $this->fetch();
          }


    
      }
     
    }
	// 财务-提现记录
    public function withdrawRecord()
    {
	if($this->request->isAjax()){
          $param = $this->request->param();
          $id  = !empty($param['id'])?intval($param['id']):''; 

          $statusType =  isset($param['status'])?$param['status']:'all'; //请求状态
          $page  = !empty($param['page'])?intval($param['page']):''; //当前页
          $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
          $start = !empty($param['start'])?strtotime($param['start']):'';//开始
          $end   = !empty($param['end'])?strtotime($param['end']):''; //结束
          $page = ($page-1)*$limit; //起始位置

 //状态筛选
           switch($statusType)
           {
      
            case '0':
               //等待充值
              
               $where[] = ['status','=',0];
            break;
            case '1':
            // 充值成功
              
                $where[] = ['status','=',1];
            break;
            case '2':
            // 充值失败
             
                $where[] = ['status','=',2];
            break; 
         

           } 
         if(!empty($id)){
            $where[] = ['id','=',$id];
         }
        $where[] = ['uid','=',session('uid')]; 
        $order['create_time'] = 'desc';
    
        $count  =model('financeWithdraw')->count($where);
        $list  = model('financeWithdraw')->listData($where,"*", $order,"$page,$limit");
         foreach($list as $k=>$v){
          $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
          $list[$k]['audit_time'] = !empty($v['audit_time'])?date('Y-m-d H:i:s',$v['audit_time']):'--';
        }
        return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$list]);
     }else{
       return $this->fetch();
     }
    }

	// 财务-流水记录
    public function record()
    {
		if($this->request->isAjax()){
          $param = $this->request->param();
          $id  = !empty($param['id'])?intval($param['id']):''; //当前页

          $statusType =  isset($param['status'])?$param['status']:'all'; //请求状态
          $page  = !empty($param['page'])?intval($param['page']):''; //当前页
          $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
          $start = !empty($param['start'])?strtotime($param['start']):'';//开始
          $end   = !empty($param['end'])?strtotime($param['end']):''; //结束
          $page = ($page-1)*$limit; //起始位置

 // //状态筛选
           switch($statusType)
           {
            case '0':             
               $where[] = ['amount','>',0];
            break;
            case '1':
                $where[] = ['amount','<',0];       // 充值成功
            break;
           }
          if(!empty($id)){
            $where[] = ['id','=',$id];
         } 
        $where[] = ['uid','=',session('uid')]; 
        $order['create_time'] = 'desc';
    
        $count  =model('financeRecord')->count($where);
        $list  = model('financeRecord')->listData($where,"*", $order,"$page,$limit");
  foreach($list as $k=>$v){
          $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
          if($v['amount'] >0){
            $list[$k]['payin'] = $v['amount'];
            $list[$k]['payout'] ='--';
          }elseif($v['amount'] < 0){
          	 $list[$k]['payin'] = '--';
             $list[$k]['payout'] = $v['amount'];
          }
         
        }
        return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$list]);
     }else{
      return $this->fetch();
     }
    }




//创建表单自动提交 用于支付提交
public function requestAutoForm($param,$url='',$method='post'){
    $html='<form action="'.$url.'"  method="'.$method.'" id="autoForm" >';
    foreach($param as $k=>$v){
    $html.='<input type="hidden" name="'.$k.'" value="'.$v.'"/>';
    }
    $html.='</form>';
    $html .='<script>document.getElementById("autoForm").submit();  
      </script>';
    return $html;
  } 
}
